<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <a-button type="primary" @click="handleCreate()"> 新增 </a-button>
      <Upload class="ml-2" :showUploadList="false" :customRequest="handleImport" accept=".json">
        <Button> 导入 </Button>
      </Upload>
    </template>
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',

              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
            {
              label: '导出',
              onClick: handleExport.bind(null, record),
            },
          ]"
        />
      </template>
    </template>
  </BasicTable>
  <modalPop @register="register" @success="handleSuccess" />
</template>

<script setup lang="ts">
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { columns, searchFormSchema } from './resourceManagement.data';
  import modalPop from './components/modalPop.vue';
  import { getResourceList, delResourceList } from '/@/api/admin/resoure';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Upload, Button } from 'ant-design-vue';
  import { addResourceServe } from '/@/api/admin/resoure';

  const { createMessage } = useMessage();

  const [register, { openModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    api: getResourceList,
    columns,
    useSearchForm: true,
    showTableSetting: false,
    bordered: false,
    formConfig: {
      labelWidth: 75,
      schemas: searchFormSchema,
    },

    actionColumn: {
      width: 160,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
    },
  });

  const handleCreate = () => {
    openModal(true, { updated: 'add' });
  };

  const handleEdit = (record) => {
    openModal(true, {
      ...record,
      updated: 'edit',
    });
  };
  // 导入json
  const handleImport = ({ file }) => {
    const reader = new FileReader();
    reader.readAsText(file);
    reader.onload = async function () {
      const bpmnData: any = this.result;
      //params.templateID = 0;
      const params = JSON.parse(bpmnData);

      await addResourceServe(params);
      createMessage.success('操作成功');
      reload();
    };
  };
  // 导出json
  const handleExport = (record) => {
    console.log(record);
    const blob = new Blob([JSON.stringify(record)]);
    let link = document.createElement('a');
    link.href = URL.createObjectURL(blob); //  创建一个 URL 对象并传给 a 的 href
    link.download = `${record.name}.json`; //  设置下载的默认文件名
    link.click();
  };
  // 删除
  const handleDelete = async (record) => {
    try {
      await delResourceList(record.id);
      createMessage.success('删除成功');
      handleSuccess();
    } catch (e) {
      throw e;
    }
  };
  const handleSuccess = () => {
    reload();
  };
</script>

<style lang="less" scoped></style>
